home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / basic / bascomm.zip / SIMCOM.BAS < prev   
BASIC Source File  |  1987-05-05  |  6KB  |  183 lines

  1. '--------------------------------------------------------------------
  2. ' PROGRAM: SIMCOM -- a SIMple COMmunications program in
  3. '  Turbo Basic.
  4. '
  5. ' AUTHOR: Reid Collins
  6. ' WRITTEN: April 25, 1987
  7. '
  8. ' DESCRIPTION:
  9. '  SIMCOM is a simple program that provides a basic level of
  10. '  full-duplex asynchronous communications support with a few
  11. '  amenities to enhance its usefulness.
  12. '
  13. '--------------------------------------------------------------------
  14.  
  15. '--------------------------------------------------------------------
  16. ' Initialization -- default values and possible override of the
  17. ' communications parameters from a disk init file.
  18. '--------------------------------------------------------------------
  19. CLEAR        ' initialize memory; clear event traps
  20. CLOSE
  21. DEFINT A-Z    ' all vars are ints unless noted otherwise
  22.  
  23. '--- Boolean values ---
  24. False = 0
  25. True = NOT False
  26.  
  27. '--- set up an error handler ---
  28. ON ERROR GOTO ErrorHandler
  29.  
  30. '--- define screen colors ---
  31. Fgnd = 2
  32. Bkgnd = 0
  33.  
  34. '--- default communications parameters ---
  35. Portnum = 1
  36. Port$ = "COM1"
  37. Rate$ = "1200"    ' specify as bits per second
  38. Parity$ = "E"   ' even parity
  39. Databits$ = "7" ' ASCII characters only
  40. Stopbits$ = "1" ' 1 or 2 stop bits
  41.  
  42. '--- set screen and cursor ---
  43. SCREEN 0, 1, 0, 0
  44. COLOR FGND, BKGND
  45. CLS
  46. LOCATE 1, 1, 1
  47.  
  48. '--- flow control variables and constants ---
  49. Paused = False
  50. PausedCount = 0
  51. WarningLevel = 128
  52. Waiting = 0    ' comm input queue size (used for testing)
  53.  
  54. '--- read init file, if any ---
  55. 10 OPEN "SIMCOM.INI" FOR INPUT AS #2
  56.    INPUT #2, Portnum, Port$, Rate$, Parity$, Databits$, Stopbits$
  57.    CLOSE #2    
  58.  
  59. '--- enable communications event trapping ---
  60. 20 IF Portnum < 0 OR Portnum > 2 THEN
  61.      PRINT "Bad port specification: COM"; Portnum
  62.      END
  63.    END IF
  64.    COM(Portnum) ON
  65.    ON COM(Portnum) GOSUB GetComInput
  66.  
  67. '--- define values for a BREAK signal ---
  68. IF Portnum = 1 THEN
  69.   PortAddr = &H3FB    ' COM1 control port address
  70. ELSE
  71.   PortAddr = &H2FB      ' COM2 control port address
  72. END IF
  73. BreakBit = &H40         ' bit pattern for BREAK on/off control
  74. BreakPeriod! = 0.5    ' seconds to hold a SPACING signal
  75.  
  76. '--- open the needed data streams ---
  77. COMPARM$ = Port$+":"+Rate$+","+Parity$+","+Databits$+","+Stopbits$
  78. PRINT "SIMCOM Parameters: " + COMPARM$
  79. OPEN COMPARM$ AS #1    ' serial port default setup
  80.  
  81. '--------------------------------------------------------------------
  82. ' Keyboard dialing reoutine -- get a number from the user and call
  83. ' the remote system (configured for Hayes-compatible tone dialing).
  84. '--------------------------------------------------------------------
  85. INPUT "Number to call: ", Number$  ' get number for the host
  86. PRINT #1, "ATDT" + Number$  ' call using tone dialing
  87.  
  88. '--------------------------------------------------------------------
  89. ' Main loop -- enter an interactive session with the remote system.
  90. ' The program spends most of its time waiting for the user to type
  91. ' something.  When data arrives at the com port, it is read and acted
  92. ' upon immediately, interrupting anything the keyboard routines may
  93. ' be doing at the time.
  94. '--------------------------------------------------------------------
  95. MainLoop:
  96.   WHILE True
  97.     ' process keyboard input, if any
  98.     IF INSTAT THEN
  99.       ' got something -- check for special keys
  100.       K$ = INKEY$
  101.       IF LEN(K$) = 2 THEN
  102.         GOSUB ExtendedCode
  103.       ELSE  ' send anything else to the remote host
  104.         PRINT #1, K$;
  105.       END IF
  106.     END IF
  107.   WEND
  108.  
  109. '--------------------------------------------------------------------
  110. ' Extended code subroutine -- process selected extended key codes.
  111. '--------------------------------------------------------------------
  112. ExtendedCode:    
  113.   SELECT CASE ASC(RIGHT$(K$, 1))  ' read the scan code
  114.   CASE 59  ' F1 key pressed -- send a hardware BREAK signal
  115.     GOSUB SendBreak
  116.   CASE 83  ' Del key pressed -- send a real ASCII DEL code
  117.     PRINT #1, CHR$(127);
  118.   CASE 117  ' Ctrl-end pressed -- return to BASIC (or DOS)
  119.             ' after reporting flow control data
  120.     CLS
  121.     PRINT PausedCount; "XOFF(s) sent to the remote system"
  122.     PRINT "Longest waiting data stream ="; Waiting; "Character(s)"
  123.     PRINT "Bye from SIMCOM"
  124.     END
  125.   END SELECT
  126.   RETURN
  127.  
  128. '--------------------------------------------------------------------
  129. ' Minimal error-handler routine -- a disk error means there is no
  130. ' init file.  Anything else causes an immediate return to the
  131. ' keyboard input routine (so the user can try to recover)
  132. '--------------------------------------------------------------------
  133. ErrorHandler:
  134.   IF 10 = ERL THEN  ' no initialization file found
  135.     PRINT "Using default communication parameters"
  136.     RESUME 20
  137.   ELSE  ' go back to reading the keyboard
  138.     RESUME MainLoop
  139.   END IF
  140.  
  141. '--------------------------------------------------------------------
  142. ' Received data subroutine -- process communications line data
  143. ' coming in from the host.
  144. '--------------------------------------------------------------------
  145. GetComInput:
  146.   IF EOF(1) THEN
  147.     IF Paused THEN
  148.       Paused = False        ' OK for host to send again
  149.       PRINT #1, CHR$(17);    ' send an XON to the host
  150.     END IF
  151.     RETURN
  152.   END IF
  153.   ' next two lines are for testing the communications input buffer
  154.   InputBufLen = LOC(1)
  155.   IF InputBufLen > WarningLevel AND not Paused THEN
  156.     Paused = TRUE        ' input buffer filling up
  157.     PRINT #1, CHR$(19);        ' send an XOFF to the host
  158.     PausedCount = PausedCount + 1
  159.   END IF
  160.   IF InputBufLen > Waiting THEN Waiting = InputBufLen
  161.   ' read from the communications buffer
  162.   I$ = INPUT$(1, #1)
  163.   IF I$ = CHR$(8) THEN
  164.     ' simulate a non-destructive backspace
  165.       IF POS(0) > 1 THEN LOCATE , POS(0) - 1
  166.   ELSEIF I$ = CHR$(13) THEN
  167.     LOCATE , 1    ' simulate a lone carriage return
  168.   ELSE
  169.     PRINT I$;    ' display anything else unchanged
  170.   END IF
  171.   GOTO GetComInput            ' see whether there's any more input
  172.  
  173. '--------------------------------------------------------------------
  174. ' BREAK signal subroutine -- holds the communication line at logical
  175. ' zero for a period of time that exceeds one normal character
  176. ' transmission period (~1/2 second is commonly used).
  177. '--------------------------------------------------------------------
  178. SendBreak:
  179.   OUT PortAddr, (INP(PortAddr) OR BreakBit)     ' set BREAK bit
  180.   DELAY BreakPeriod!
  181.   OUT PortAddr, (INP(PortAddr) AND NOT BreakBit) ' clear BREAK bit
  182.   RETURN
  183.